//
//  RBMsgVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/7/13.
//

#import "RBMsgVC.h"
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import "UIScrollView+RBAdd.h"
#import "RBSearchTagView.h"
#import "RBThemeConfig.h"
#import <Masonry.h>
#import "RBNavigationBar.h"
#import "RBTextField.h"
#import "UIButton+RBAdd.h"
#import <UIColor+YYAdd.h>
#import <UIView+YYAdd.h>
#import "RBMsgCell.h"
#import "RBMsgTransactionVC.h"
#import "RBMsgAtyVC.h"
#import "RBMsgSystemVC.h"
#import "RBMsgResponse.h"
#import "UIViewController+RBAdd.h"
#import "RBRefreshHeader.h"
#import "RBRefreshFooter.h"
#import "RBMsgAtyDetailVC.h"
#import "RBMsgSystemDetailVC.h"

@interface RBMsgVC ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray *dataSource;
@property(nonatomic, strong)IBOutlet UIView *msgView;
@property(nonatomic, strong)IBOutlet UIStackView *msgStackView;

@property(nonatomic, strong) RBMsgResponse *response;

@end

@implementation RBMsgVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
    [self.tableView.mj_header beginRefreshing];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    __weak typeof(self) weak_self = self;
    [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
        dicM[@"page"] = @"1";
    } path:@"han_xiaoxi_index" post:YES log:YES responseClass:RBMsgResponse.class desView:self.view showHud:NO showPlaceHolder:NO sucBlock:^(RBMsgResponse *data) {
        for (NSInteger i = 0; i < weak_self.msgStackView.arrangedSubviews.count; i++)
        {
            UIView *itemView = weak_self.msgStackView.arrangedSubviews[i];
            NSInteger count = 0;
            if(i == 0)
            {
                count = data.data.jiaoyi;
            }
            else if(i == 1)
            {
                count = data.data.xitong;
            }
            else if(i == 2)
            {
                count = data.data.huodong;
            }
            UILabel *numL = (UILabel *)[itemView viewWithTag:3];
            numL.textAlignment = NSTextAlignmentCenter;
            numL.hidden = count <= 0;
            numL.text = [NSString stringWithFormat:@"%zd", count];
        }
    } failBlock:nil reloadBlock:nil];
}

#pragma mark - Private

- (void)setup
{
    self.title = @"系统消息";
    [self.msgView zoomSubViewsWithSelf];
    self.msgView.height = Zoom(self.msgView.height);
    for (NSInteger i = 0; i < self.msgStackView.arrangedSubviews.count; i++)
    {
        UIView *itemView = self.msgStackView.arrangedSubviews[i];
        itemView.tag = i;
        [itemView rbAddTapGesWithTarget:self action:@selector(msgViewTapped:)];
        NSInteger count = 0;
        UILabel *numL = (UILabel *)[itemView viewWithTag:3];
        numL.textAlignment = NSTextAlignmentCenter;
        numL.hidden = count <= 0;
        numL.text = [NSString stringWithFormat:@"%zd", count];
    }
}

- (void)fillData
{
    [self.dataSource removeAllObjects];
    [self.dataSource addObject:({
        RBGroupModel *group = RBGroupModel.new;
        group.headerView = self.msgView;
        group.headerHeight = self.msgView.height;
        group;
    })];
    [self.dataSource addObject:({
        RBGroupModel *group = RBGroupModel.new;
        group.headerView = ({
            UIView *view = UIView.new;
            view.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
            view;
        });
        group.headerHeight = Zoom(10);
        group;
    })];
    [self.dataSource addObject:({
        RBGroupModel *group = RBGroupModel.new;
        group.cellModels = ({
            NSMutableArray *arrayM = NSMutableArray.array;
            arrayM;
        });
        group;
    })];
    [self appendData];
}

- (void)appendData
{
    __weak typeof(self) weak_self = self;
    NSMutableArray *arrayM = ({
        RBGroupModel *group = self.dataSource.lastObject;
        (NSMutableArray *)group.cellModels;
    });
    for (id model in self.response.data.data)
    {
        [arrayM addObject:({
            RBCellModel *cellModel = RBCellModel.new;
            cellModel.reuseCellIdentifier = NSStringFromClass(RBMsgCell.class);
            cellModel.cellData = model;
            cellModel.cellSelectBlock = ^(NSIndexPath *indexPath) {
                RBMsgListModel *model = [RanTool modelWithSection:indexPath.section row:indexPath.row dataSource:weak_self.dataSource];
                /** 类型 1 系统通知 2 活动优惠 3 交易通知     */
                if(model.type == 1)
                {
                    RBMsgSystemDetailVC *vc = RBMsgSystemDetailVC.new;
                    vc.id = model.id;
                    [weak_self.navigationController pushViewController:vc animated:YES];
                }
                else if(model.type == 2)
                {
                    RBMsgAtyDetailVC *vc = RBMsgAtyDetailVC.new;
                    vc.id = model.id;
                    [weak_self.navigationController pushViewController:vc animated:YES];
                }
            };
            cellModel;
        })];
    }
    [self.tableView reloadData];
    if(!arrayM.count)
    {
//        [self.tableView showPlaceHolder:RB_EMPTY_TIP tapBlock:nil];
    }
}

#pragma mark - Network

- (NSString *)listPageKey
{
    return @"page";
}
- (NSDictionary *)listParams
{
    return @{
        
    };
}
- (NSString *)listRequestPath
{
    return @"han_xiaoxi_index";
}
- (NSInteger)ListCurrentPage
{
    return self.response.currentPage;
}
- (BOOL)listLog
{
    return YES;
}
- (BOOL)listPost
{
    return YES;
}
- (Class)listResponseClass
{
    return RBMsgResponse.class;
}
- (UIScrollView *)listView
{
    return self.tableView;
}

- (void)getFirstPageData
{
    __weak typeof(self) weak_self = self;
    [self listLoadFirstPageWithDesView:self.tableView showPlaceHolder:YES sucBlock:^(id data) {
        weak_self.response = data;
        [weak_self fillData];
    } failBlock:^(id data) {
        
    } reloadBlock:^{
        [weak_self getFirstPageData];
    }];
}

- (void)getNextPageData
{
    __weak typeof(self) weak_self = self;
    [self listLoadNextPageWithSucBlock:^(id data) {
        weak_self.response = data;
        [weak_self appendData];
    } failBlock:^(id data) {
        
    }];
}

#pragma mark - Event

- (void)msgViewTapped: (UITapGestureRecognizer *)ges
{
    switch (ges.view.tag)
    {
        case 0:
        {
            RBMsgTransactionVC *vc = RBMsgTransactionVC.new;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 1:
        {
            RBMsgSystemVC *vc = RBMsgSystemVC.new;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 2:
        {
            RBMsgAtyVC *vc = RBMsgAtyVC.new;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        default:
            break;
    }
}

#pragma mark - UITableViewDataSource & Delegate

kRBTableViewDataSourceAndDelegate(self.dataSource);

#pragma mark - Setter

#pragma mark - Getter

- (UITableView *)tableView
{
    if (!_tableView)
    {
        self.tableView = ({
            UITableView *view = [UITableView.alloc initWithFrame:CGRectZero style:UITableViewStylePlain];
            view.dataSource = self;
            view.delegate = self;
            view.showsVerticalScrollIndicator = NO;
            view.showsHorizontalScrollIndicator = NO;
            kRBIgnoreScrollViewInsetsAdjust(view);
            view.separatorInset = UIEdgeInsetsMake(0, Zoom(68), 0, Zoom(12));
            view.separatorColor = [UIColor colorWithHexString:@"#F8F8F8"];
            view.tableFooterView = UIView.new;
            view.estimatedRowHeight = UITableViewAutomaticDimension;
            view.mj_header = [RBRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(getFirstPageData)];
            view.mj_footer = [RBRefreshFooter footerWithRefreshingTarget:self refreshingAction:@selector(getNextPageData)];

            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.mas_equalTo(0);
                make.top.mas_equalTo(0);
            }];
            view;
        });
    }
    return _tableView;
}

- (NSMutableArray *)dataSource
{
    if (!_dataSource)
    {
        self.dataSource = NSMutableArray.array;
    }
    return _dataSource;
}


@end
